package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2023/10/13 10:19
 * @apiNote
 */

public class Test00679MaximumSwap {


    public static void main(String[] args) {

    }

    public static int test(int num) {
        char[] charArray = String.valueOf(num).toCharArray();
        int n = charArray.length;
        int maxNum = num;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                swap(charArray, i, j);
                maxNum = Math.max(maxNum, Integer.parseInt(new String(charArray)));
                swap(charArray, i, j);
            }
        }
        return maxNum;
    }

    public static void swap(char[] charArray, int i, int j) {
        char temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
    }


    public static int test1(int num) {
        char[] charArray = String.valueOf(num).toCharArray();
        int n = charArray.length;
        //最大值的下标
        int maxIdx = n - 1;
        //idx1 最大值左边的最小的   idx2  最大的值
        int idx1 = -1, idx2 = -1;
        for (int i = n - 1; i >= 0; i--) {
            if(charArray[i] > charArray[maxIdx]){
                maxIdx = i;
            }else if(charArray[i] < charArray[maxIdx]){
                idx1 = i;
                idx2 = maxIdx;
            }
        }
        if(idx1 >= 0){
            swap(charArray, idx1, idx2);
            return Integer.parseInt(new String(charArray));
        }else{
            return num;
        }
    }

}
